package com.banksoft.cps.controller;

import com.banksoft.cps.bean.BuyCompany;
import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.Register;
import com.banksoft.cps.bean.TransportCompany;
import com.banksoft.cps.enums.UserType;
import com.banksoft.cps.service.BuyCompanyService;
import com.banksoft.cps.service.CompanyService;
import com.banksoft.cps.service.RegisterService;
import com.banksoft.cps.service.TransportCompanyService;
import com.banksoft.cps.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by rongzhao.zhu on 2018/8/31.
 */
@Controller
@RequestMapping("/front/company")
public class FrontCompanyController extends ControllerConstants {

    @Autowired
    RegisterService registerService;
    @Autowired
    TransportCompanyService transportCompanyService;
    @Autowired
    BuyCompanyService buyCompanyService;
    @Autowired
    CompanyService companyService;
    @RequestMapping(value = "register", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(Register register, HttpServletRequest request,HttpServletResponse response){
        try {
            registerService.register(register);
            if(UserType.company.name().equals(register.getUserType())){
                Company user =  companyService.login(register.getAccount(),register.getPassword());
                CookieUtil.setLoginCookie(response,user,"");
            }else if(UserType.trans.name().equals(register.getUserType())){
                TransportCompany user =  transportCompanyService.login(register.getAccount(),register.getPassword());
                CookieUtil.setLoginCookie(response,user,"");
            }else if(UserType.buy.name().equals(register.getUserType())){
                BuyCompany user =  buyCompanyService.login(register.getAccount(),register.getPassword());
                CookieUtil.setLoginCookie(response,user,"");
            }

            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

}
